package com.xiaoq.sites.endpoint;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.RedisTemplate4JedisCluster;
import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.sites.entity.ActShowEntity;
import com.xiaoq.sites.service.IActShowService;
import com.xiaoq.sites.vo.ActShowVo;
import com.xiaoq.vo.PagingVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/api/v1/sites")
@Api(tags={"今日活动相关Api"})
public class ActShowEndpoint {
	
	@Autowired
	private IdGenerator idGenerator;
	
	@Autowired
	private RedisTemplate4JedisCluster redisTemplate;
	

	@Autowired
	private IActShowService actShowSer;

	private static final class ActShowListResponse extends GenericListResponse<ActShowVo> {
	};
	
	private  class ActShowDataResponse extends GenericDataResponse<ActShowVo> {}
	

	private Logger logger = LoggerFactory.getLogger(ActShowEndpoint.class);

	@ApiOperation(value = "活动列表", notes = "根据景区ID查询活动列表")
	@ApiResponses({ @ApiResponse(code = 200, response = ActShowListResponse.class, message = "活动列表") })
	@GetMapping(value = "/actshow", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> listBySites(
			@RequestParam(value = "orgnizerId", required = false) @ApiParam(value = "orgnizerId") String orgnizerId,
			@RequestParam(value = "time", required = false) @ApiParam(value = "time yyyy/MM/dd这个类型的long值", required = false) String time,
			@ModelAttribute(value="pvo")PagingVo pvo) {
		
		Long curTime = 0L;
		if (time != null) {
			curTime = Long.parseLong(time);
		} else {
			curTime = System.currentTimeMillis();
		}
		List<ActShowEntity> actShowList = actShowSer.findActShow(orgnizerId, pvo, curTime);
		Long total = (Long) pvo.getTc();
		List<ActShowVo> actShowVoList = new ArrayList<>();
		ActShowVo actShowVo = null;
		if (CollectionUtils.isNotEmpty(actShowList)) {
			for (ActShowEntity actShow : actShowList) {
				actShowVo = new ActShowVo();
				BeanUtils.copyProperties(actShow, actShowVo);
				actShowVoList.add(actShowVo);

			}
			return ActShowListResponse.listAndCount(actShowVoList, total);
		} else {

			logger.debug("actShowList 返回值为空！");
			return ActShowListResponse.listNoCount(actShowVoList);
		}

	}
	
	@ApiOperation(value = "获取管理员活动列表", notes = "获取管理员活动列表")
	@ApiResponses({ @ApiResponse(code = 200, response = ActShowListResponse.class, message = "获取管理员活动列表") })
	@GetMapping(value = "/actshow/ofmgr", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> listByMgr(PagingVo pvo,HttpServletRequest request) {
		String userId = (String) request.getAttribute("userId");
		String shopId = redisTemplate.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, userId);
		List<ActShowEntity> actShowList = actShowSer.findActShow(shopId, pvo, null);
		List<ActShowVo> actShowVoList = new ArrayList<>();
		ActShowVo actShowVo = null;
		if(actShowList!=null){
			for(ActShowEntity actShowEntity:actShowList){
				actShowVo = new ActShowVo();
				BeanUtils.copyProperties(actShowEntity, actShowVo);
				actShowVoList.add(actShowVo);
			}
		}
		Long total = (Long) pvo.getTc();
		return ActShowListResponse.listAndCount(actShowVoList, total);

	}

	@ApiOperation(value = "今日活动发布", notes = "今日活动发布")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "今日活动发布") })
	@PostMapping(value = "/actshow", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addActShow(
			@RequestBody @ApiParam(value = "actShowVo", required = true) ActShowVo actShowVo,HttpServletRequest request) {
		String userId = (String) request.getAttribute("userId");
		String shopId = redisTemplate.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, userId);
		if(StringUtils.isEmpty(shopId)){
			return GenericResponse.ng("你还不是一个店铺的管理员！");
		}
		//这个判断没有任何意义
		if (actShowVo != null) {
			ActShowEntity actShowEntity = new ActShowEntity();
			BeanUtils.copyProperties(actShowVo, actShowEntity);
			actShowEntity.setId(idGenerator.nextId());
			actShowEntity.setStatus("1");
			actShowEntity.setOrgnizerId(shopId);
			actShowEntity.setOrginzerType("10");
			actShowEntity.setPubTime(System.currentTimeMillis());
			boolean result = actShowSer.addActShow(actShowEntity);
			if (result) {
				return GenericResponse.ok();
			}
		}
		return GenericResponse.ng("添加失败！");

	}

	@ApiOperation(value = "删除一条今日活动", notes = "删除一条今日活动")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "删除一条今日活动") })
	@DeleteMapping(value = "/actshow", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> delActShow(String actShowId) {
		boolean result = actShowSer.delActShow(actShowId);
		if (result) {
			return GenericResponse.ok();
		} else {
			return GenericResponse.ng("删除失败！");
		}

	}

	@ApiOperation(value = "修改一条今日活动", notes = "修改一条今日活动")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "修改一条今日活动") })
	@PutMapping(value = "/actshow", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> editActShow(
			@RequestBody ActShowVo actShowVo) {
		ActShowEntity actShowEntity = new ActShowEntity();
		BeanUtils.copyProperties(actShowVo, actShowEntity);
		boolean result = actShowSer.editActShow(actShowEntity);
		if (result) {
			return GenericResponse.ok();
		}
		return GenericResponse.ng("修改失败！");
	}
	
	@ApiOperation(value = "活动详情", notes = "活动详情")
	@ApiResponses({ @ApiResponse(code = 200, response = ActShowDataResponse.class, message = "活动详情") })
	@GetMapping(value = "/actshow/detail", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getActDetail(@RequestParam(value="actId",required=true)@ApiParam(value="活动Id",required=true)String actId) {
		
		ActShowEntity actShow = actShowSer.getActShowDetail(actId);
		ActShowVo actShowVo = new ActShowVo();
		if(actShow!=null){
			BeanUtils.copyProperties(actShow, actShowVo);
		}
		return ActShowDataResponse.okWithData(actShow);
		
	}

}
